package com.think.teaching.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.College;
import com.think.teaching.model.School;
import com.think.teaching.service.impl.CollegeServiceImpl;
import com.think.teaching.service.impl.SchoolServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 学校 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "学校管理接口")
public class SchoolController {
	@Autowired
	private SchoolServiceImpl schoolServiceImpl;

	@ApiOperation("添加新的学校")
	@PostMapping("admin/school")
	public Result AddNewSchool(@RequestBody() School data) {
		Result result = data.CheckObjectComplete();
		if (schoolServiceImpl.getSchoolWithName(data.getName())!=null) {
			return Result.failure(ResultCode.SCHOOL_NAME_DUL);
		}
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			School school = schoolServiceImpl.AddObjectEntity(data);
			if (school != null) {
				return Result.success(school);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新的学校")
	@RequestMapping(value = "admin/school", method = RequestMethod.PUT)
	public Result UpdateSchool(@RequestBody() School data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			School school = schoolServiceImpl.UpdateSchool(data);
			if (school != null) {
				return Result.success(school);
			} else {
				return Result.failure(ResultCode.SCHOOL_NOT_EXIST);
			}
		} else
			return result;
	}

	@ApiOperation("删除学校")
	@RequestMapping(value = "admin/school/{id}", method = RequestMethod.DELETE)
	public Result DeleteSchool(@PathVariable("id") Integer id) {
		School school = schoolServiceImpl.getById(id);
		if (school == null) {
			return Result.failure(ResultCode.SCHOOL_NOT_EXIST);
		} else {
			if (schoolServiceImpl.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取学校详情")
	@RequestMapping(value = "admin/school/{id}", method = RequestMethod.GET)
	public Result GetSchool(@PathVariable("id") Integer id) {
		School school = schoolServiceImpl.getById(id);
		if (school == null) {
			return Result.failure(ResultCode.SCHOOL_NOT_EXIST);
		} else {

			return Result.success(school);
		}
	}

	@ApiOperation("管理员修改学校状态")
	@RequestMapping(value = "admin/school/state", method = RequestMethod.PUT)
	public Result UpdateSchoolState(@RequestParam("id") Integer id, @RequestParam("state") Integer state) {
		School school = schoolServiceImpl.getById(id);
		if (school == null) {
			return Result.failure(ResultCode.SCHOOL_NOT_EXIST);
		} else {
			try {
				schoolServiceImpl.ChangeObjectState(id, state);
				return Result.success(school);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索学校")
	@RequestMapping(value = "user/school/search", method = RequestMethod.GET)
	public Result UserSearchCollege(@RequestParam("key") String key) {
		return Result.success(schoolServiceImpl.SearchSchoolWithKey(key, true));
	}
	
	@ApiOperation("用户获取学校下的所有院系")
	@RequestMapping(value = "user/school/colleges/{id}", method = RequestMethod.GET)
	public Result UserGetSchoolColleges(@PathVariable("id") Integer id) {
		School school = schoolServiceImpl.getById(id);
		if (school == null) {
			return Result.failure(ResultCode.SCHOOL_NOT_EXIST);
		} else {
			try {
				return Result.success(schoolServiceImpl.getSchoolColleges(id));
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("管理员搜索院系")
	@RequestMapping(value = "admin/school/search", method = RequestMethod.GET)
	public Result AdminSearchSchool(@RequestParam("key") String key) {
		return Result.success(schoolServiceImpl.SearchSchoolWithKey(key, true));
	}

	@ApiOperation("管理员搜索院系列表")
	@RequestMapping(value = "admin/schools/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListSchool(@PathVariable("page") Integer page, @PathVariable("count") Integer count) {
		return Result.success(schoolServiceImpl.ListSchoolWithTagAndType(count, page,false));
	}

	@ApiOperation("用户搜索院系列表")
	@RequestMapping(value = "user/schools/{page}/{count}", method = RequestMethod.GET)
	public Result UserListSchool(@PathVariable("page") Integer page, @PathVariable("count") Integer count) {
		return Result.success(schoolServiceImpl.ListSchoolWithTagAndType(count, page, true));
	}
}
